package com.gs.core.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Aspect
public class AppProfiler {
	private int order;

	  // allows us to control the ordering of advice
	  public int getOrder() {
	    return this.order;
	  }

	  public void setOrder(int order) {
	    this.order = order;
	  }
	  
	  // this method is the around advice
	  @Around("execution(!void com.gs.*..*Service.*(..))")
	  public Object profile(ProceedingJoinPoint call) throws Throwable {
	    Object returnValue;
	    StopWatch clock = new StopWatch(getClass().getName());
	    try {
	      clock.start(call.toShortString());
	      returnValue = call.proceed();
	    } finally {
	      clock.stop();
	      System.out.println(clock.prettyPrint());
	    }
	    return returnValue;
	  }
	}
